﻿using System;
using System.Text;
using System.Drawing;
using System.Buffers;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;

public static partial class glDRIVE
{
    /*
    函数 gl.trch
    Toeplitz矩阵求逆
    参数 t: 存放T型矩阵中的元素t[0]～t[n-1]
    参数 tt: 后n-1个元素存放T型矩阵中tt[1]～tt[n-1]
    参数 n: 
    参数 b: 返回T型矩阵的逆矩阵
    返回值 0表示失败，大于0表示成功
    */

    public static string drive_trch()
    {
        int n, i, j, k;
        double[] t = new double[6] { 10.0, 5.0, 4.0, 3.0, 2.0, 1.0 };
        double[] tt = new double[6] { 0.0, -1.0, -2.0, -3.0, -4.0, -5.0 };
        double[,] b = new double[6, 6];

        n = 6;
        string rs = "";
        rs += gl.html_table("Mat T:", t);
        rs += gl.html_table("Mat TT:", tt);
        i = gl.trch(t, tt, n, b);
        if (i < 0) return "error: " + i;

        double[,] a = new double[6, 6];
        for (i = 1; i <= 6; i++)
        {
            for (j = 1; j <= 6; j++)
            {
                a[i - 1, j - 1] = 0.0;
                for (k = 1; k <= j - 1; k++)
                {
                    a[i - 1, j - 1] += b[i - 1, k - 1] * t[j - k];
                }
                a[i - 1, j - 1] += b[i - 1, j - 1] * t[0];
                for (k = j + 1; k <= 6; k++)
                {
                    a[i - 1, j - 1] += b[i - 1, k - 1] * tt[k - j];
                }
            }
        }

        rs += gl.html_table("Mat B=inv(T):", b);
        rs += gl.html_table("Mat A=T*B:", a);
        return rs;
    }
}